//
//  CTCAnswerResponderVC.m
//  ConsultantVersion
//
//  Created by 李小龙 on 2017/3/27.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "CTCAnswerResponderVC.h"

@interface CTCAnswerResponderVC ()
@property (weak, nonatomic) IBOutlet BWHTextView *answerTextView;

@end

@implementation CTCAnswerResponderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    
    [self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupNavBar
{
    self.title = @"回答";
    
    if (self.fromType == CTCAnswerResponderFromTypeRapidList) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    }
    
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemClick)];
}


- (void)setupUI
{
    self.answerTextView.placeHolderTextFont = [UIFont systemFontOfSize:13];
    self.answerTextView.placeHolderText = @"请输入您的答案......";
    self.answerTextView.placeHolderTextColor = HEXCOLOR(0x979797);
}


- (void)rightBarButtonItemClick
{
    
    if (!self.answerTextView || !self.answerTextView.text.length) {
        [MBProgressHUD showTipMessageInView:@"请输入您的回答"];
        return;
    }
    
    
    [self.view endEditing:YES];
    
    
    NSDictionary *paramDic = @{
                               @"uuid":self.uuid,
                               @"content":self.answerTextView.text
                               };
    
    NSString *url = nil;
    
    if (self.fromType == CTCAnswerResponderFromTypeO2OList) {
        url = kSubmoitO2OAnswerUrl;
    }
    else {
        url = kSubmoitRespondeAnswerUrl;
    }
    
//    [MBProgressHUD showAnimationHUD];;
    
    [RequestTool POST:url
           parameters:paramDic
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  DeLog(@"responseObject %@", responseObject);
                  
                  NSInteger code = [responseObject[@"code"] integerValue];
                  NSString *des = responseObject[@"des"];
                  if (code == 1) {
                      [MBProgressHUD showSuccessMessage:des];
                      if (self.answerSuccessBlock) {
                          self.answerSuccessBlock();
                      }
                      
                      [[NSNotificationCenter defaultCenter] postNotificationName:kReloadO2OListRedBadgeNotification object:nil];
                      
                      [[NSNotificationCenter defaultCenter] postNotificationName:kReloadRepaidHistoryListRedBadgeNotification object:nil];
                      
                      [self.navigationController popViewControllerAnimated:YES];
                  }
                  else {
                      [MBProgressHUD showErrorMessage:des];
                      
                      if (code == 2) {
                          CTCLoginViewController *vc = [CTCLoginViewController viewControllerFromStoryboard];
                          [self presentViewController:vc animated:YES completion:nil];
                      }
                  }
              } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  DeLog(@"error %@", error);
                  if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
                      [MBProgressHUD showErrorMessage:@"网络不给力，请检查网络"];
                  }
                  else {
                      [MBProgressHUD showErrorMessage:@"服务器繁忙"];
                  }
              }];
}

@end
